package com.free.front.controller.product;

import javax.servlet.http.HttpServletRequest;

import com.deco.domain.vo.product.DicVehicleVo;
import com.free.common.vo.ResultModel;
import com.free.ext.shiro.SessionManager;
import com.free.front.bf.product.ProductSecurityBf;
import com.free.front.controller.BaseController;

import com.google.code.kaptcha.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
@Controller
public class ProductSecurityController extends BaseController {
	
	@Autowired
	private ProductSecurityBf productSecurityBf;
    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/

    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/
    @RequestMapping(value="/securityLabel.htm")
    public String securityLabel(HttpServletRequest request){
        return getTempleate("/prod/securityLabel",request);
    }

    @RequestMapping(value="/wechatresult.htm")
    public String wechatresult(HttpServletRequest request){
        return getTempleate("/prod/securityLabel",request);
    }

    @RequestMapping(value="/securityLabelRes.htm")
    public String securityLabelRes(HttpServletRequest request){
        return getTempleate("/prod/securityLabelRes",request);
    }

    @RequestMapping(value="/resultCcnRedirect.json")
    @ResponseBody
    public ResultModel resultCcnRedirect(HttpServletRequest request,String code){
        if(StringUtils.isEmpty(code) || code.length()!= 16){
            return ResultModel.failure(null,"请输入16位防伪标签码");
        }
        String ip = null;
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getRemoteAddr();
        } else {
            ip = request.getHeader("x-forwarded-for");
        }
        String[] ips = ip.split(",");
        ip = ips[0];
        return ResultModel.success(productSecurityBf.getCnnResult(ip,code,"20"));
    }

    @RequestMapping(value="/resultCcn.json")
    @ResponseBody
    public ResultModel resultCcn(HttpServletRequest request,String code, String yzm){
        if(StringUtils.isEmpty(code) || code.length()!= 16){
            return ResultModel.failure(null,"请输入16位防伪标签码");
        }
        if(StringUtils.isEmpty(yzm) || yzm.length()!= 4){
            return ResultModel.failure(null,"请输入4位验证码");
        }
        Object verifyCode = request.getSession().getAttribute("verifyCode");
        if(verifyCode == null || !verifyCode.toString().equals(yzm)){
            return ResultModel.failure(null,"您输入的验证码不正确");
        }
    	 String ip = null;
         if (request.getHeader("x-forwarded-for") == null) {
        	 ip = request.getRemoteAddr();
         } else {
        	 ip = request.getHeader("x-forwarded-for");
         }
         String[] ips = ip.split(",");
    	 ip = ips[0];
        return ResultModel.success(productSecurityBf.getCnnResult(ip,code,"10"));
    }


	
    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/

    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/
}
